package com.supinfo.food.services;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.supinfo.food.dao.DaoFactory;
import com.supinfo.food.dao.UserDao;
import com.supinfo.food.entities.User;

/**
 * User Service
 * @author Keitaro
 */
@WebService(name="User", serviceName="User")
public class UserService {

	private UserDao dao = DaoFactory.getUserDao();
	
	@WebMethod(operationName="connexion")
	public User	connexion(@WebParam(name="login") String login, @WebParam(name="password") String password) {
		User user = dao.get(login);
		if (user != null) {
			if (user.getPassword().equals(password))
				return user;
		}
		return null;
	}
	
	@WebMethod(operationName="add")
	public boolean	add(@WebParam(name="user") User user) {
		User result = dao.get(user.getLogin());
		if (result != null) {
			return false;
		}
		dao.persist(user);
		return true;
	}
	
	@WebMethod(operationName="getAll")
	public User[]	getAll() {
		return dao.getAll().toArray(new User[1]);
	}
	
	@WebMethod(operationName="delete")
	public void	delete(Long id) {
		dao.delete(id);
	}
}
